package com.zhao.api;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.zhao.constant.ApiConstant;
import com.zhao.vo.Act;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

/**
 * 聚推客API
 *
 * @author zhaoxing
 * @date 2022/5/16
 */

@RestController
public class jtkApi {

    //http://api.jutuike.com/union/act_list
    @GetMapping("/act_list")
    public void actList(){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("apikey", "M92OGj6sdlX8JXvqfA1ngVCljRd3G1cF");
        paramMap.put("page", 1);
        paramMap.put("pageSize", 100);
        String json = HttpUtil.get(ApiConstant.JTK_ACT_LIST, paramMap);

        System.out.println(json);

        JSONObject jsonObject = JSONObject.parseObject(json);
        json = jsonObject.getString("data");
        jsonObject = JSONObject.parseObject(json);
        System.out.println(jsonObject.getString("data"));
        List<Act> list = JSONObject.parseArray(jsonObject.getString("data"), Act.class);

        //list.stream().sorted(new Comparator<Act>() {
        //    @Override
        //    public int compare(Act a1, Act a2) {
        //        return a1.getActId() > a2.getActId() ? 1 : 0;
        //    }
        //});

        Collections.sort(list, new Comparator<Act>() {
            @Override
            public int compare(Act o1, Act o2) {
                return o1.getActId() > o2.getActId() ? 1 : -1;
            }
        });

        list.stream().forEach(a->{
            System.out.println(a);
        });

    }


    //http://api.jutuike.com/union/orders
    @GetMapping("/order_list")
    public void ordersList(){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("apikey", "M92OGj6sdlX8JXvqfA1ngVCljRd3G1cF");
        paramMap.put("page", 1);
        paramMap.put("pageSize", 100);
        String json = HttpUtil.get(ApiConstant.JTK_ORDER_LIST, paramMap);

        System.out.println(json);

        JSONObject jsonObject = JSONObject.parseObject(json);
        json = jsonObject.getString("data");
        jsonObject = JSONObject.parseObject(json);
        System.out.println(jsonObject.getString("data"));
        List<Act> list = JSONObject.parseArray(jsonObject.getString("data"), Act.class);

        //list.stream().sorted(new Comparator<Act>() {
        //    @Override
        //    public int compare(Act a1, Act a2) {
        //        return a1.getActId() > a2.getActId() ? 1 : 0;
        //    }
        //});

        Collections.sort(list, new Comparator<Act>() {
            @Override
            public int compare(Act o1, Act o2) {
                return o1.getActId() > o2.getActId() ? 1 : -1;
            }
        });

        list.stream().forEach(a->{
            System.out.println(a);
        });

    }

    //http://api.jutuike.com/union/orders
    @GetMapping("/hdk")
    public void hdk(){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("apikey", "0E5E2A915E7B");
        paramMap.put("sort", 1);
        String json = HttpUtil.get("http://v2.api.haodanku.com/pdd_hot_rank", paramMap);
        System.out.println(json);
    }

}
